/*
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: HistoryEntrustOrderFragment.java
 *   @Date: 18-11-29 下午4:05
 *   @Author: ppzhao
 *   @Description:
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 */

package io.bhex.app.account.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import io.bhex.app.R;
import io.bhex.baselib.utils.PixelUtils;
import io.bhex.sdk.trade.bean.OrderBean;
import io.bhex.app.account.presenter.HistoryEntrustFragmentPresenter;
import io.bhex.app.base.BaseFragment;
import io.bhex.app.utils.DateUtils;
import io.bhex.app.utils.IntentUtils;
import io.bhex.app.utils.KlineUtils;

/**
 * ================================================
 * 描   述：委托订单
 * ================================================
 */

public class HistoryEntrustOrderFragment extends BaseFragment<HistoryEntrustFragmentPresenter, HistoryEntrustFragmentPresenter.HistoryEntrustOrderUI> implements HistoryEntrustFragmentPresenter.HistoryEntrustOrderUI, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {
    private RecyclerView recyclerView;
    private HistoryEntrustAdapter adapter;
    private SmartRefreshLayout swipeRefresh;
    private View emptyView;

    @Override
    protected HistoryEntrustFragmentPresenter.HistoryEntrustOrderUI getUI() {
        return this;
    }

    @Override
    protected HistoryEntrustFragmentPresenter createPresenter() {
        return new HistoryEntrustFragmentPresenter();
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_entrust_order_layout, null, false);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    protected void initViews() {
        super.initViews();
        swipeRefresh = viewFinder.find(R.id.swipeRefresh);
        recyclerView = viewFinder.find(R.id.recyclerView);

        Bundle arguments = getArguments();
        if (arguments != null) {
            String symbol = arguments.getString("symbol");
        }

        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        emptyView = layoutInflater.inflate(R.layout.empty_layout, swipeRefresh, false);
        ViewGroup.LayoutParams layoutParams = emptyView.getLayoutParams();
        layoutParams.height = PixelUtils.dp2px(200);
        emptyView.setLayoutParams(layoutParams);

    }

    @Override
    protected void addEvent() {
        super.addEvent();
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void onLoadMoreRequested() {
        getPresenter().loadMore();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        getPresenter().getHistoryEntrustOrders(false);
        refreshLayout.finishRefresh(1000);
    }

    @Override
    public void loadMoreComplete() {
//        if (swipeRefresh.isRefreshing()) {
//            swipeRefresh.setRefreshing(false);
//        }
        if (adapter != null) {
            adapter.loadMoreComplete();
        }
    }

    @Override
    public void loadMoreFailed() {
        if (adapter != null) {
            adapter.loadMoreFail();
        }
    }

    @Override
    public void loadEnd() {
        if (adapter != null) {
            adapter.loadMoreEnd();
        }
    }

    @Override
    public void showOrders(List<OrderBean> currentOrders) {
        if (adapter == null) {

            adapter = new HistoryEntrustAdapter(getActivity(), currentOrders);
//            adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            adapter.isFirstOnly(false);
            adapter.setOnLoadMoreListener(this,recyclerView);
            adapter.setEmptyView(emptyView);

//            swipeRefresh.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
//            swipeRefresh.setOnRefreshListener(this);

            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));

            recyclerView.setAdapter(adapter);
        } else {
            adapter.setNewData(currentOrders);
        }
    }

    public static class HistoryEntrustAdapter extends BaseQuickAdapter<OrderBean,BaseViewHolder> {

        private Context mContext;

        public HistoryEntrustAdapter(Context context, List<OrderBean> data) {
            super(R.layout.item_history_entrust_order_layout, data);
            mContext = context;
        }

        @Override
        protected void convert(final BaseViewHolder baseViewHolder, final OrderBean itemModel) {
            baseViewHolder.setVisible(R.id.item_divider, baseViewHolder.getAdapterPosition() != mData.size());
            String title = itemModel.getBaseTokenName() + " / " + itemModel.getQuoteTokenName();
            baseViewHolder.setText(R.id.order_name, title);
            baseViewHolder.setTextColor(R.id.order_type,KlineUtils.getBuyOrSellColor(mContext,itemModel.getSide()));
            baseViewHolder.setText(R.id.order_type,KlineUtils.getPriceModeTxt(mContext, itemModel.getType()) +" " + KlineUtils.getBuyOrSellTxt(mContext, itemModel.getSide()));
            baseViewHolder.setText(R.id.order_status, KlineUtils.getOrderStatus(mContext,itemModel));
            baseViewHolder.setText(R.id.order_time, DateUtils.getSimpleTimeFormat(Long.valueOf(itemModel.getTime()), "HH:mm:ss yyyy/MM/dd"));
            baseViewHolder.setText(R.id.order_price_title, mContext.getString(R.string.string_format_price));
            baseViewHolder.setText(R.id.order_price, KlineUtils.getPrice(mContext,itemModel));
            baseViewHolder.setText(R.id.order_deal_average_price_title, mContext.getString(R.string.string_format_deal_average_price));
            baseViewHolder.setText(R.id.order_deal_average_price, KlineUtils.getAvgPrice(mContext,itemModel));
            baseViewHolder.setText(R.id.order_entrust_amount_title,  KlineUtils.getEntrustTitle(mContext,itemModel)+":");
            baseViewHolder.setText(R.id.order_entrust_amount, KlineUtils.getOrderEntrustAndUnit(itemModel));
            baseViewHolder.setText(R.id.order_deal_amount_title, mContext.getString(R.string.string_format_deal_amount));
            baseViewHolder.setText(R.id.order_deal_amount, KlineUtils.getDealAmount(mContext,itemModel));
            baseViewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtils.goOrderDetail(mContext,itemModel);
                }
            });
        }

    }
}
